import Vue from 'vue'

 const state = {
    socket: {
      isConnected: false,
      message: '',
      reconnectError: false,
    }
}

const mutations = {
    //打开链接
    SOCKET_ONOPEN : (state,event) =>  {
      Vue.prototype.$socket = event.currentTarget
      state.socket.isConnected = true
    },
    //关闭链接
    SOCKET_ONCLOSE : (state,event) =>  {
      state.socket.isConnected = false
    },
    //打开失败
    SOCKET_ONERROR : (state,event) =>   {
      console.error(state, event)
    },
    // 接收消息
    SOCKET_ONMESSAGE : (state,message) =>   {
      console.log(message)
      state.socket.message = message
    },
    // 重新链接
    SOCKET_RECONNECT : (state,count) =>  {
      console.info(state, count)
    },
    //重连失败
    SOCKET_RECONNECT_ERROR: (state) =>  {
      state.socket.reconnectError = true;
    },
}


const actions = {
    reg({ commit,state,dispatch }, context) {
      console.log(context,'1111111111111');
      var message = {
            controller:'Index',
            action:'reg',
            event:'reg',
            data:context
          }
      dispatch('sendMessage',message);
    },
    sendMessage: function({ commit,state,dispatch },context) {
      console.log(context,'safasdfasdfasf');
      Vue.prototype.$socket.send(JSON.stringify(context))
    }
}


export default {
  namespaced: true,
  state,
  mutations,
  actions
}
